/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.ieslaferreria.creditsintesis.logicgame;

import com.jme.math.FastMath;
import edu.ieslaferreria.creditsintesis.logicgame.actions.RmiEvent;
import edu.ieslaferreria.creditsintesis.logicgame.actions.Runner;
import edu.ieslaferreria.creditsintesis.logicgame.statemachineimpl.FirstExplorerStateMachine;
import edu.ieslaferreria.creditsintesis.rmi.GameEvent;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Clase para crear un objeto con movimiento propio y con su respectiva máquina
 * de estados
 * @author josep
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

public class FirstExplorer extends MobilObject{
    //private float maxTimeStopped = FastMath.nextRandomInt(5, 20);
    //private int maxTimesDoing = FastMath.nextRandomInt(1, 5);
    //private float maxTimeStopped = 15;
    private int maxTimesDoing = FastMath.nextRandomInt(513, 5313); //-1;
    private int timesDoing = 0;
    private Timer timer = null;
    int colour;



   public FirstExplorer(Runner runner, int col, ActionStates as){
       super(new FirstExplorerStateMachine(), runner, as);
       this.colour=col;
       prv_init();
    }

   public FirstExplorer(String pnam , Runner runner, int col, ActionStates as){
       super(pnam, new FirstExplorerStateMachine(), runner, as);
       this.colour=col;
       prv_init();
    }

   private void prv_init(){
       timer = new Timer();
       this.processTransition(FirstExplorerStateMachine.TR_STOP);
   }

    @Override
    public void processTransition(int transition){
        super.processTransition(transition);
        if(this.getState()==FirstExplorerStateMachine.ST_STOPPED){
            int maxTimeStopped = FastMath.nextRandomInt(5, 10);
            System.out.println("Sliping for: " + maxTimeStopped);
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    //processTransition(FirstExplorerStateMachine.TR_MAX_TIME);
                    processTransition(FirstExplorerStateMachine.ST_TO_FORWARD);
                }
            }, (maxTimeStopped*1000));
        }else if(maxTimesDoing>0 && transition==FirstExplorerStateMachine.TR_MAX_TIMES){
            maxTimesDoing = FastMath.nextRandomInt(513, 5313);
        }
    }

    @Override
    public RmiEvent updateAction(float time){
        if(maxTimesDoing>0){
            timesDoing++;
            if(getTimesDoing()>=getMaxTimesDoing()){
                this.processTransition(FirstExplorerStateMachine.TR_MAX_TIMES);
                timesDoing=0;
            }
        }
        return super.updateAction(time);
    }


    /**
     * @return the maxTimesDoing
     */
    public int getMaxTimesDoing() {
        return maxTimesDoing;
    }

    /**
     * @return the timesDoing
     */
    public int getTimesDoing() {
        return timesDoing;
    }

    /**
     * @param timesDoing the timesDoing to set
     */
    public void setTimesDoing(int timesDoing) {
        this.timesDoing = timesDoing;
    }

    /**
     * @return the actionToDo
     */
    @Override
    public int getActionToDo() {
        return stateToAction[this.getState()%5];
    }

}
